package inet.app.tuvi.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ImageUtil {
	
	private final static String TAG = "image_util";

	@SuppressWarnings("deprecation")
	public static void scaleImage(ImageView view, int boundBoxInDp) {
		// Get the ImageView and its bitmap
		Drawable drawing = view.getDrawable();
		Bitmap bitmap = ((BitmapDrawable) drawing).getBitmap();

		// Get current dimensions
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();

		// Determine how much to scale: the dimension requiring less scaling is
		// closer to the its side. This way the image always stays inside your
		// bounding box AND either x/y axis touches it.
		float xScale = ((float) boundBoxInDp) / width;
		float yScale = ((float) boundBoxInDp) / height;
		float scale = (xScale <= yScale) ? xScale : yScale;

		// Create a matrix for the scaling and add the scaling data
		Matrix matrix = new Matrix();
		matrix.postScale(scale, scale);

		// Create a new bitmap and convert it to a format understood by the
		// ImageView
		Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height,
				matrix, true);
		BitmapDrawable result = new BitmapDrawable(scaledBitmap);
		width = scaledBitmap.getWidth();
		height = scaledBitmap.getHeight();

		// Apply the scaled bitmap
		view.setImageDrawable(result);

		// Now change ImageView's dimensions to match the scaled image
		ViewGroup.LayoutParams params = view.getLayoutParams();
		params.width = width;
		params.height = height;
		view.setLayoutParams(params);
	}

	public static Bitmap scaleImageFullScreen(Activity activity, Bitmap bitmap) {
		if (bitmap == null) {
			return null;
		}

		int width = DeviceUtil.getDeviceInfo(activity).width;
		int height = DeviceUtil.getDeviceInfo(activity).height;
		
		float scaleX = (float) width / bitmap.getWidth();
		float scaleY = (float) height / bitmap.getHeight();
		
		Matrix matrix = new Matrix();
		matrix.postScale(scaleX, scaleY);
		
		Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0,
				bitmap.getWidth(), bitmap.getHeight(), matrix, true);
		
		return scaledBitmap;
	}

	public static Bitmap scaleImageFullScreen(Activity activity, int id) {
		Bitmap bm = BitmapFactory.decodeResource(activity.getResources(), id);
		return scaleImageFullScreen(activity, bm);
	}

	public static Bitmap scaleImage(Activity activity, int id, int newWidth,
			int newHeight) {
		Bitmap bitmap = BitmapFactory.decodeResource(activity.getResources(),
				id);
		return Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false);
	}

	public static Bitmap scaleBitmap(Activity activity, int id, int widthScale,
			int heightScale) {
		Bitmap bitmap = BitmapFactory.decodeResource(activity.getResources(),
				id);
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();

		float scaleW = (float) widthScale / width;
		float scaleH = (float) heightScale / height;

		Matrix matrix = new Matrix();
		matrix.postScale(scaleW, scaleH);

		Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height,
				matrix, true);

		return scaledBitmap;

	}

	public static Bitmap scaleImage(Activity activity, int id) {
		Resources resources = activity.getResources();

		// Decode image size
		BitmapFactory.Options o = new BitmapFactory.Options();
		o.inJustDecodeBounds = true;
		BitmapFactory.decodeResource(resources, id, o);

		int width = DeviceUtil.getDeviceInfo(activity).width;
		int height = DeviceUtil.getDeviceInfo(activity).height;

		Log.d(TAG, "ws = " + width + ", hs = " + height);

		// Find the correct scale value. It should be the power of 2.
		int scale = 1;
		while (o.outWidth / scale / 2 >= width
				&& o.outHeight / scale / 2 >= width)
			scale *= 2;

		Log.d(TAG, "scale = " + scale);

		// Decode with inSampleSize
		BitmapFactory.Options o2 = new BitmapFactory.Options();
		o2.inSampleSize = scale;

		return BitmapFactory.decodeResource(resources, id, o2);
	}

	public static Bitmap getThumbnailImage(String filePath) throws IOException {
		FileOutputStream out;
		// image file use to create image u can give any path.
		File land = new File(Environment.getExternalStorageDirectory()
				.getAbsoluteFile() + "/portland.jpg");

		// filePath is your video file path.
		Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(filePath,
				MediaStore.Video.Thumbnails.FULL_SCREEN_KIND);
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
		byte[] byteArray = stream.toByteArray();

		out = new FileOutputStream(land.getPath());
		out.write(byteArray);
		out.close();

		return bitmap;

	}
}
